package LeetCode.String;

public class Test8 {

    //转化为字符串比较
    public static boolean isPalindrome(int x) {
        if (x < 0) {
            return false;
        }
        String str = x + "";
        int len = str.length();
        for (int i = 0; i < len / 2 + 1; i++) {
            if (str.charAt(i) != str.charAt(len - 1 - i)) {
                return false;
            }
        }
        return true;
    }

    //不将整数转为字符串来解决这个问题
    public static boolean isPalindrome2(int x) {
        if (x < 0) {
            return false;
        }
        int count = 0;
        int y = x; //位数
        while (y >= 1) {
            y /= 10;
            count++;
        }
//        System.out.println("位数count =" + count);
//        System.out.println("for count / 2 + 1 =" + ((count / 2) + 1));

        for (int i = 1; i < (count / 2) + 1; i++) {
            //次方运算
//            System.out.println(Math.pow(10, (count - i)));
            //取整
//            System.out.println("i=" + i);
//            //低位 取余后取整
//            System.out.println("低位");
//            System.out.println(x % (int) Math.pow(10, i) / (int) Math.pow(10, i - 1));
//            //高位  取整后取余
//            System.out.println("高位");
//            System.out.println( x / (int) Math.pow(10, count-i) % (int) Math.pow(10, 1));
            if (x % (int) Math.pow(10, i) / (int) Math.pow(10, i - 1) !=
                    x / (int) Math.pow(10, count-i) % (int) Math.pow(10, 1)) {
                return false;
            }
        }
        return true;
    }


    public static void main(String[] args) {
        System.out.println(isPalindrome2(1));
        System.out.println(isPalindrome2(190163385));
    }
}
